package com.campus.dao.impl;

import com.campus.dao.ClassDao;
import com.campus.model.MyClass;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author HLQ
 * @date 2024/11/17 21:09
 */
@Slf4j
@Repository
public class ClassDaoImpl implements ClassDao {
    @Autowired
    JdbcTemplate jdbcTemplate;

    private final RowMapper<MyClass> classRowMapper = new RowMapper<MyClass>() {
        @Override
        public MyClass mapRow(ResultSet rs, int rowNum) throws SQLException {
            MyClass myClass = new MyClass()
                    .setId(rs.getInt("id"))
                    .setName(rs.getString("name"))
                    .setCreateTime(rs.getTime("create_time"))
                    .setClassDesc(rs.getString("class_desc"))
                    .setTeacherId(rs.getInt("teacher_id"))
                    .setActivityId(rs.getInt("activity_id"));
            return myClass;
        }
    };
    @Override
    public int addClass(MyClass myClass) {
        String sql = "INSERT INTO t_manager_class " +
                "(name, create_time, class_desc, teacher_id, activity_id) " +
                "VALUES (?,?,?,?,?)";
        Object[] args = new Object[]{
                myClass.getName(),
                myClass.getCreateTime(),
                myClass.getClassDesc(),
                myClass.getTeacherId(),
                myClass.getActivityId()
        };
        return jdbcTemplate.update(sql, args);
    }
    @Override
    public int deleteClassById(int id) {
        String sql = "DELETE FROM t_manager_class WHERE id = ?";
        return jdbcTemplate.update(sql, id);
    }
    @Override
    public int updateClassById(MyClass myClass) {
        String sql = "UPDATE t_manager_class SET " +
                "name = ?, create_time = ?, class_desc = ?, teacher_id = ?, activity_id = ? WHERE id = ?";
        Object[] args = new Object[]{
                myClass.getName(),
                myClass.getCreateTime(),
                myClass.getClassDesc(),
                myClass.getTeacherId(),
                myClass.getActivityId(),
                myClass.getId()
        };
        return jdbcTemplate.update(sql, args);
    }
    @Override
    public MyClass queryClassById(int id) {
        String sql = "SELECT * FROM t_manager_class WHERE id = ?";
        return jdbcTemplate.queryForObject(sql, new Object[]{id}, classRowMapper);
    }

    @Override
    public MyClass queryClassByName(String name) {
        String sql = "SELECT * FROM t_manager_class WHERE name = ?";
        return jdbcTemplate.queryForObject(sql, new Object[]{name}, classRowMapper);
    }
    @Override
    public List<MyClass> queryClassByActivity(Integer activityId){
        String sql = "SELECT * FROM t_manager_class WHERE activity_id = ?";
        return jdbcTemplate.query(sql,new Object[]{activityId},classRowMapper);
    }
    @Override
    public int addClassStudent(int classId, int studentId, int groupId){
        String sql = "INSERT INTO t_teaching_student_in_class " +
                "(class_id, student_id,group_id) " +
                "values(?,?,?)";
        return jdbcTemplate.update(sql,new Object[]{classId,studentId,groupId});
    }
    @Override
    public int addClassTeacher(int classId,int teacherId){
        String sql = "Update t_manager_class set teacher_id = ? where id = ?";
        return jdbcTemplate.update(sql,new Object[]{teacherId,classId});
    }

}
